﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NewsMine.Storage.SQLite
{
    public class SQLiteQueue : IPersistedQueue
    {

        static object syncSingleObject = new object();

        #region public API

        public void Enqueue(object obj)
        {
            lock (syncSingleObject)
            {
                InsertObjectInSqlite(obj);
            }
        }

    

        public void EnqueueMultiple(object[] objects)
        {
            throw new NotImplementedException();
        }

        public object Dequeue()
        {
            throw new NotImplementedException();
        }

        public object[] DequeueMultiple(int count)
        {
            throw new NotImplementedException();
        }

        public void ClearQueue()
        {
            throw new NotImplementedException();
        }

        public int GetCount()
        {
            throw new NotImplementedException();
        }
        #endregion

        #region SQLite related stuff
        //connnection info

        SQLite.SQLiteDatabase sqliteDatabase;

        //opening connection

        public SQLiteQueue(string queueName)
        {
            if (string.IsNullOrEmpty(queueName))
                throw new ApplicationException("Invalid queue to use");

            //get the configured keyvaluestore base directory.
            //and append the queueName as the file name.
            string sqliteFileName = Path.Combine(Configuration.ConfigurationManager.KeyValueStoreBaseFolder, queueName + ".db3");

            if (File.Exists(sqliteFileName))
            {
                // Open the sql connection.
                sqliteDatabase = new SQLiteDatabase(sqliteFileName);
            }
            else
            {
                // Open the sql connection.
                sqliteDatabase = new SQLiteDatabase(sqliteFileName);
                MakeSureTableIsExists();
            }
        }


        //creating required tables

        private void MakeSureTableIsExists()
        {
            string createTableScript = "CREATE TABLE [queue] ([item] BLOB  NOT NULL)";
        }

        //Inserting data (one / many records) into sqlite table with transaction 
        private void InsertObjectInSqlite(object obj)
        {
           
        }


        //Selecting record(s) ( 1 / many) from database and also we need to delete them from the database




        #endregion


    }
}
